<template>
  <el-dialog
    v-model="authModal.visible"
    :title="authModal.mode === 'login' ? '登录' : '注册'"
    :width="isMobile ? '90vw' : '520px'"
    :close-on-click-modal="false"
    append-to-body
    @closed="onClosed"
    class="auth-dialog"
    :class="{ 'mobile': isMobile }"
  >
    <div class="reason-tip" v-if="authModal.reason">
      <el-alert :title="authModal.reason" type="warning" center :closable="false" />
    </div>

    <div class="switcher">
      <el-button-group :size="isMobile ? 'large' : 'default'">
        <el-button
          :type="segmented === 'login' ? 'primary' : 'default'"
          @click="segmented = 'login'"
          class="switcher-btn"
          >登录</el-button
        >
        <el-button
          :type="segmented === 'register' ? 'primary' : 'default'"
          @click="segmented = 'register'"
          class="switcher-btn"
          >注册</el-button
        >
      </el-button-group>
    </div>

    <div v-show="segmented === 'login'" class="form-container">
      <LoginForm @success="handleLoginSuccess" :compact="!isMobile" />
    </div>
    <div v-show="segmented === 'register'" class="form-container">
      <RegisterForm @success="handleRegisterSuccess" :compact="!isMobile" />
    </div>

    <template #footer>
      <div class="footer-actions">
        <el-button 
          @click="authModal.close()" 
          :size="isMobile ? 'large' : 'default'"
          class="cancel-btn"
        >
          取消
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, watch } from 'vue'
import { useRouter } from 'vue-router'
import { useAuthModalStore } from '@/stores/authModal'
import { useResponsive } from '@/utils/responsive'
import LoginForm from './LoginForm.vue'
import RegisterForm from './RegisterForm.vue'

const router = useRouter()
const authModal = useAuthModalStore()
const segmented = ref<'login' | 'register'>(authModal.mode)

// 使用响应式工具检测移动端
const { isMobile } = useResponsive()

watch(
  () => authModal.mode,
  (m) => {
    segmented.value = m
  },
)
watch(segmented, (m) => {
  authModal.mode = m
})

const onClosed = () => {
  // 保持 redirect 值，以便登录成功后跳转
}

const handleLoginSuccess = () => {
  authModal.close()
  if (authModal.redirect) {
    router.push(authModal.redirect)
  }
}

const handleRegisterSuccess = () => {
  // 注册成功后默认切回登录
  segmented.value = 'login'
}
</script>

<style scoped>
.reason-tip {
  margin-bottom: 12px;
  text-align: center;
}

.switcher {
  display: flex;
  justify-content: center;
  margin-bottom: 12px;
}

.switcher-btn {
  min-width: 80px;
}

.form-container {
  min-height: 200px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.footer-actions {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 16px 0;
}

.cancel-btn {
  min-width: 100px;
}

/* 按钮样式优化 - 更方正 */
.switcher-btn {
  border-radius: 0 !important;
  font-weight: 600;
  transition: all 0.3s ease;
  border: 2px solid transparent;
  position: relative;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.switcher-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.switcher-btn:not(.el-button--primary) {
  background: white;
  color: #666;
  border-color: #e9ecef;
}

.switcher-btn:not(.el-button--primary):hover {
  background: #f8f9fa;
  color: #333;
  border-color: #dee2e6;
}

.switcher-btn.el-button--primary {
  background: linear-gradient(135deg, #ff5a00 0%, #ff0036 100%);
  border-color: transparent;
  color: white;
  box-shadow: 0 4px 12px rgba(255, 90, 0, 0.3);
}

.switcher-btn.el-button--primary:hover {
  box-shadow: 0 6px 20px rgba(255, 90, 0, 0.4);
}

.cancel-btn {
  border-radius: 0 !important;
  font-weight: 600;
  transition: all 0.3s ease;
  border: 2px solid #e9ecef;
  color: #666;
  background: white;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.cancel-btn:hover {
  border-color: #dee2e6;
  color: #333;
  background: #f8f9fa;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

/* 按钮组样式优化 */
.switcher :deep(.el-button-group) {
  border-radius: 0;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.switcher :deep(.el-button-group .el-button) {
  border-radius: 0;
  margin-left: -1px;
}

.switcher :deep(.el-button-group .el-button:first-child) {
  border-radius: 0;
  margin-left: 0;
}

.switcher :deep(.el-button-group .el-button:last-child) {
  border-radius: 0;
}

/* 对话框整体居中样式 */
.auth-dialog :deep(.el-dialog) {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.auth-dialog :deep(.el-dialog__body) {
  width: 100%;
  text-align: center;
}

/* 移动端适配样式 */
.auth-dialog.mobile {
  --el-dialog-padding-primary: 16px;
}

.auth-dialog.mobile :deep(.el-dialog__header) {
  padding: 16px 16px 8px;
}

.auth-dialog.mobile :deep(.el-dialog__body) {
  padding: 8px 16px 16px;
}

.auth-dialog.mobile :deep(.el-dialog__footer) {
  padding: 0 16px 16px;
}

.auth-dialog.mobile .switcher {
  margin-bottom: 16px;
}

.auth-dialog.mobile .switcher-btn {
  min-width: 100px;
  height: 44px;
  font-size: 16px;
  border-radius: 0 !important;
}

.auth-dialog.mobile .form-container {
  min-height: 240px;
}

.auth-dialog.mobile .footer-actions {
  padding: 20px 0;
}

.auth-dialog.mobile .cancel-btn {
  min-width: 120px;
  height: 44px;
  font-size: 16px;
  border-radius: 0 !important;
}

/* 触摸优化 */
@media (hover: none) and (pointer: coarse) {
  .switcher-btn,
  .cancel-btn {
    transition: all 0.2s ease;
  }
  
  .switcher-btn:active,
  .cancel-btn:active {
    transform: scale(0.98);
  }
}

/* 小屏幕优化 */
@media (max-width: 480px) {
  .auth-dialog.mobile {
    --el-dialog-padding-primary: 12px;
  }
  
  .auth-dialog.mobile :deep(.el-dialog__header) {
    padding: 12px 12px 6px;
  }
  
  .auth-dialog.mobile :deep(.el-dialog__body) {
    padding: 6px 12px 12px;
  }
  
  .auth-dialog.mobile :deep(.el-dialog__footer) {
    padding: 0 12px 12px;
  }
  
  .switcher-btn {
    min-width: 90px;
    height: 40px;
    font-size: 15px;
    border-radius: 0 !important;
  }
  
  .cancel-btn {
    min-width: 100px;
    height: 40px;
    font-size: 15px;
    border-radius: 0 !important;
  }
}

/* 横屏模式优化 */
@media (max-height: 500px) and (orientation: landscape) {
  .auth-dialog.mobile .form-container {
    min-height: 180px;
  }
  
  .auth-dialog.mobile .switcher {
    margin-bottom: 8px;
  }
  
  .auth-dialog.mobile .reason-tip {
    margin-bottom: 8px;
  }
}
</style>
